﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using DomainDriver.DomainModeling.DataProviders;

namespace DomainDriver.DomainModeling.Configuration
{
    public class DomainDriverConfiguration : ConfigurationSection
    {
        public const string ConnectionToUserPropertyName = "ConnectionToUse";
        public const string DefaultConnectionToUse = "DomainDriverDatabase";

        [ConfigurationProperty(ConnectionToUserPropertyName, DefaultValue = DefaultConnectionToUse, IsRequired = true)]
        public string ConnectionToUse
        {
            get
            {
                return (string)this[ConnectionToUserPropertyName];
            }
            set
            {
                this[ConnectionToUserPropertyName] = value;
            }
        }

        public const string PersistenceTypeToUsePropertyName = "PersistenceTypeToUse";
        public const DataSourcePersistenceType DefaultPersistenceTypeToUse = DataSourcePersistenceType.None;

        [ConfigurationProperty(PersistenceTypeToUsePropertyName, DefaultValue = DefaultPersistenceTypeToUse, IsRequired = true)]
        public DataSourcePersistenceType PersistenceTypeToUse
        {
            get
            {
                return (DataSourcePersistenceType)this[PersistenceTypeToUsePropertyName];
            }
            set
            {
                this[PersistenceTypeToUsePropertyName] = value;
            }
        }
    }

    public static class DomainDriverConfigurationHelper
    {
        public const string RecommendedGroupName = "DomainDriver";
        public const string RecommendedSectionName = "DomainDriverConfiguration";

        private static string s_ConnectionToUse = null;
        private static DataSourcePersistenceType s_PersistenceTypeToUse = DataSourcePersistenceType.None;

        static DomainDriverConfigurationHelper()
        {
            string[] sectionPathsToTry = new string[] { (RecommendedGroupName + "/" + RecommendedSectionName), RecommendedSectionName, RecommendedGroupName };

            try
            {
                foreach (string path in sectionPathsToTry)
                {
                    object configurationSection = ConfigurationManager.GetSection(path);

                    if (configurationSection == null)
                    { continue; }

                    DomainDriverConfiguration typedConfigurationSection = (DomainDriverConfiguration)configurationSection;
                    s_ConnectionToUse = typedConfigurationSection.ConnectionToUse;
                    s_PersistenceTypeToUse = typedConfigurationSection.PersistenceTypeToUse;
                }
            }
            catch (Exception exception)
            {
                s_ConnectionToUse = null;
                s_PersistenceTypeToUse = DataSourcePersistenceType.None;
            }
        }

        public static string ConnectionToUse
        {
            get { return s_ConnectionToUse; }
        }

        public static DataSourcePersistenceType PersistenceTypeToUse
        {
            get { return s_PersistenceTypeToUse; }
        }
    }
}